/**
 * 快速创建组件
 */

const lodash = require('lodash');


function FormBase(label = '', { ui, widgetConfig = {} } = {}) {
  /**
   * 快速创建base
   * @param label
   * @param ui
   * @param widgetConfig
   * @returns {{ui: {widget: string, label: string, widgetConfig: {type: string}}, type: string}}
   * @constructor
   */
  return {
    type: 'string',
    ui: {
      label,
      widgetConfig: {
        ...widgetConfig,
      },
      ...ui,
    },
  };
}

function FormInput(label = '', { ui, widgetConfig = {} } = {}) {
  /**
   * 快速创建input
   * @param label
   * @param ui
   * @param widgetConfig
   * @returns {{ui: {widget: string, label: string, widgetConfig: {type: string}}, type: string}}
   * @constructor
   */
  return {
    type: 'string',
    ui: { widget: 'CusInput',
      label,
      widgetConfig: {
        ...widgetConfig,
      },
      ...ui,
    },
  };
}

function FormTextarea(...args) {
  /**
   * 创建textarea
   * @param args
   * @returns {{ui: {widget: string, label: string, widgetConfig: {type: string}}, type: string}}
   * @constructor
   */
  const ret = FormInput(...args);
  ret.ui.widgetConfig.type = 'textarea';
  return ret;
}


function FormInputNativeNumber(label = '', { ui, widgetConfig = {} } = {}) {
  /**
   * 快速创建input number
   * @param label
   * @param ui
   * @param widgetConfig
   * @returns {{ui: {widget: string, label: string, widgetConfig: {type: string}}, type: string}}
   * @constructor
   */
  return {
    type: 'number',
    ui: { widget: 'CusInput',
      label,
      widgetConfig: {
        type: 'number',
        ...widgetConfig,
      },
      ...ui,
    },
  };
}


function FormInputNativeInteger(...args) {
  /**
   * 创建integer
   * @param args
   * @returns {{ui: {widget: string, label: string, widgetConfig: {type: string}}, type: string}}
   * @constructor
   */
  const ret = FormInputNativeNumber(...args);
  ret.ui.widgetConfig.subType = 'integer';
  return ret;
}


function FormSelect(label = '', opt = {}) {
  /**
   * 创建select
   * @param label
   * @param opt
   * @returns {{ui: {widget: string, label: string, widgetConfig: {type: string}}, type: string}}
   * @constructor
   */
  const ret = FormBase(label, opt);
  const options = lodash.get(opt, 'options', []);
  ret.ui.widget = 'CusSelect';
  ret.ui.widgetConfig.options = options;
  return ret;
}

function FormRadio(label = '', opt = {}) {
  /**
   * 创建radio
   * @param label
   * @param opt
   * @returns {{ui: {widget: string, label: string, widgetConfig: {type: string}}, type: string}}
   * @constructor
   */
  const ret = FormBase(label, opt);
  const options = lodash.get(opt, 'options', []);
  ret.ui.widget = 'CusRadio';
  ret.ui.widgetConfig.options = options;
  return ret;
}

function FormCheckbox(label = '', opt = {}) {
  /**
   * 创建checkbox
   * @param label
   * @param opt
   * @returns {{ui: {widget: string, label: string, widgetConfig: {type: string}}, type: string}}
   * @constructor
   */
  const ret = FormBase(label, opt);
  const options = lodash.get(opt, 'options', []);
  ret.ui.widget = 'CusCheckbox';
  ret.ui.widgetConfig.options = options;
  return ret;
}

function FormDatePicker(label = '', opt = {}) {
  /**
   * 快速创建DatePicker
   * @param label
   * @param opt
   * @returns {{ui: (*&{label: string, widgetConfig: {}}), type: string}}
   * @constructor
   */
  return FormBase(label, {
    ui: {
      widget: 'CusDatePicker',
    },
  });
}

function FormSimpleUploader(label = '', opt = {}) {
  /**
   * 快速创建SimpleUploader
   * @param label
   * @param opt
   * @returns {{ui: (*&{label: string, widgetConfig: {}}), type: string}}
   * @constructor
   */
  const ret = FormBase(label, opt);
  ret.ui.widget = 'CusSimpleUploader';
  return ret;
}


function FormImgUploader(label = '', opt = {}) {
  /**
   * 快速创建ImgUploader
   * @param label
   * @param opt
   * @returns {{ui: (*&{label: string, widgetConfig: {}}), type: string}}
   * @constructor
   */
  const ret = FormBase(label, opt);
  ret.ui.widget = 'CusImgUploader';
  return ret;
}

function FormInputNumber(label = '', opt = {}) {
  /**
   * 快速创建ImgUploader
   * @param label
   * @param opt
   * @returns {{ui: (*&{label: string, widgetConfig: {}}), type: string}}
   * @constructor
   */
  const ret = FormBase(label, opt);
  ret.ui.widget = 'CusInputNumber';
  return ret;
}

function FormColorPicker(label = '', opt = {}) {
  /**
   * 快速创建ImgUploader
   * @param label
   * @param opt
   * @returns {{ui: (*&{label: string, widgetConfig: {}}), type: string}}
   * @constructor
   */
  const ret = FormBase(label, opt);
  ret.ui.widget = 'CusColorPicker';
  return ret;
}

module.exports = {
  FormInput,
  FormTextarea,
  FormInputNativeInteger,
  FormInputNumber,
  FormColorPicker,
  FormSelect,
};
